//
//  SCComposeView.swift
//  weibo
//
//  Created by 阿城 on 15/12/13.
//  Copyright © 2015年 阿城. All rights reserved.
//

import UIKit

class SCComposeView: UIView {
    
    var btns = [SCComposeButton]()
    
    var target: UIViewController?
    
    func show(target: UIViewController){
        self.target = target
        target.view.addSubview(self)
        buttonAnimation()
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
        frame = UIScreen.mainScreen().bounds
        
        let bgImg = UIImage.shotScreen().applyLightEffect()
        let bgView = UIImageView(image: bgImg)
        addSubview(bgView)
        addSubview(sloganImageView)
        
        // 添加约束
        sloganImageView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self)
            make.top.equalTo(self).offset(100)
        }
        
        loadButton()
        
    }
    
    //加载按钮
    private func loadButton(){
        
        let path = NSBundle.mainBundle().pathForResource("compose.plist", ofType: nil)!
        let dataList = NSArray(contentsOfFile: path)!
        
        let btnW:CGFloat = 80
        let margin:CGFloat = (kScreenWidth - btnW * CGFloat(3)) / 4
        
        for i in 0..<6 {
            let btn = SCComposeButton(dict: dataList[i] as! [String : AnyObject])
            
            let col = i % 3
            let row = i / 3
            let btnX = margin + (margin + btnW) * CGFloat(col)
            let btnY = (margin + btnW) * CGFloat(row) + kScreenHeight - 300
            btn.frame = CGRect(x: btnX, y: btnY, width: btnW, height: btnW)
            
            //bug 规避
            btn.transform = CGAffineTransformMakeTranslation(0, 300)
            
            addSubview(btn)
            btns.append(btn)
            btn.addTarget(self, action: "buttonClick:", forControlEvents: UIControlEvents.TouchUpInside)
        }
        
    }
    
    
    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        buttonAnimation()
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (Int64)(0.6 * Double(NSEC_PER_SEC))), dispatch_get_main_queue(), { () -> Void in
            //移除
            self.removeFromSuperview()
        })
        
    }
    
    private func buttonAnimation(){
        
        let maxIdx = btns.count - 1
        
        for (i,btn) in btns.enumerate(){
            
            if !CGAffineTransformIsIdentity(btn.transform) {
                //上升动画
                UIView.animateWithDuration(0.25, delay: 0.08 * Double(i), usingSpringWithDamping: 0.7, initialSpringVelocity: 8, options: [], animations: { () -> Void in
                    
                    btn.transform = CGAffineTransformIdentity
                    
                    }, completion: nil)
            }else{
                
                //下降动画
                UIView.animateWithDuration(0.25, delay: 0.08 * Double(maxIdx - i), usingSpringWithDamping: 0.7, initialSpringVelocity: 8, options: [], animations: { () -> Void in
                    
                    btn.transform = CGAffineTransformMakeTranslation(0, 300)
                    
                    }, completion: nil)
                
            }
        }
        
    }
    
    private func buttonAnimation1(){
        
        let maxIdx = btns.count - 1
        
        for (i,btn) in btns.enumerate(){
            
            if CGAffineTransformIsIdentity(btn.transform) {
                //上升动画
                UIView.animateWithDuration(0.25, delay: 0.08 * Double(i), usingSpringWithDamping: 0.7, initialSpringVelocity: 8, options: [], animations: { () -> Void in
                    
                    btn.transform = CGAffineTransformMakeTranslation(0, -300)
                    
                    }, completion: nil)
            }else{
                
                //下降动画
                UIView.animateWithDuration(0.25, delay: 0.08 * Double(maxIdx - i), usingSpringWithDamping: 0.7, initialSpringVelocity: 8, options: [], animations: { () -> Void in
                    
                    btn.transform = CGAffineTransformIdentity
                    
                    }, completion: nil)
                
            }
        }
        
    }
    
    @objc private func buttonClick(sender: SCComposeButton){
        
        for btn in btns{
            
            if btn == sender{
                UIView.animateWithDuration(0.25, animations: { () -> Void in
                    
                    btn.transform = CGAffineTransformScale(btn.transform, 2, 2)
                    btn.alpha = 0.2
                    
                    }, completion: { (_) -> Void in
                        self.removeFromSuperview()
                        
                        let vcType = NSClassFromString(sender.classname!) as? UIViewController.Type
                        
                        let vc = vcType!.init()
                        
                        self.target!.presentViewController(UINavigationController(rootViewController: vc), animated: true, completion:nil)
                })
                
            }else{
                UIView.animateWithDuration(0.25, animations: { () -> Void in
                    
                    btn.transform = CGAffineTransformScale(btn.transform, 0.3, 0.3)
                    btn.alpha = 0.2
                    
                })
            }
 
        }
    }
    
    private lazy var composeVc: SCComposeViewController = SCComposeViewController()
    // 宣传语的图片
    private lazy var sloganImageView: UIImageView = UIImageView(image: UIImage(named: "compose_slogan"))
    
}
